package dominio;

import interfaces.Archivo;
import interfaces.BaseDeDatos;

import java.io.IOException;
import java.io.Serializable;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

@SuppressWarnings("serial")
public class Avion implements Serializable, BaseDeDatos {
	private Integer idAvion;
	private String marcaAvion;
	private String modeloAvion;
	private Integer capacidadAvion;
	
	public Avion(){
		
	}
	
	public Avion(Integer id, String marca, String modelo, Integer capacidad){
		this.idAvion = id;
		this.marcaAvion = marca;
		this.modeloAvion = modelo;
		this.capacidadAvion = capacidad;
	}
	
	public Integer getIdAvion() {
		return idAvion;
	}
	public void setIdAvion(Integer idAvion) {
		this.idAvion = idAvion;
	}
	public String getMarcaAvion() {
		return marcaAvion;
	}
	public void setMarcaAvion(String marcaAvion) {
		this.marcaAvion = marcaAvion;
	}
	public String getModeloAvion() {
		return modeloAvion;
	}
	public void setModeloAvion(String modeloAvion) {
		this.modeloAvion = modeloAvion;
	}
	public Integer getCapacidadAvion() {
		return capacidadAvion;
	}
	public void setCapacidadAvion(Integer capacidadAvion) {
		this.capacidadAvion = capacidadAvion;
	}
	
	public String toString(){
		return  marcaAvion + ", " + modeloAvion;
	}

	@Override
	public List<Avion> leerRegistrosBD() throws SQLException {
		List<Avion> lista = new ArrayList<Avion>();	
		Connection conn = null;
		String query = "Select * FROM aviones";
		conn = DriverManager.getConnection(host + dbName, user, pass);
		Statement st = conn.createStatement();			
		ResultSet rs = st.executeQuery(query);
		while(rs.next()){
			lista.add(new Avion(rs.getInt("idavion_a"), rs.getString("marca_a"),
					rs.getString("modelo_a"), rs.getInt("capacidad_a") ));
		}
		if(conn != null)conn.close();
		return lista;
	}

	@Override
	public void guardarRegistroBd() throws SQLException {
		Connection conn = null;
		conn = DriverManager.getConnection(host + dbName, user, pass);
		Statement st = conn.createStatement();		
		String query = "INSERT INTO aviones(marca_a,modelo_a,capacidad_a) "
				+ "VALUES (" + "'"
				+ this.getMarcaAvion() + "','"
				+ this.getModeloAvion() + "',"
				+ this.getCapacidadAvion()
				+ ");";
		st.executeUpdate(query);		
		if(conn != null)conn.close();
		
	}
}
